import { Component, OnInit } from '@angular/core';
import { Router,ActivatedRoute,NavigationExtras } from '@angular/router';
import {HttpClient,HttpHeaders} from "@angular/common/http";
import {environment} from 'src/environments/environment';
import { ToastController } from '@ionic/angular';
import { EncryptService } from '@services/encrypt.service';
import { AuthTokenService } from '@services/auth-token.service';
import { CommonResponse,VerificationType,ToastColor,CommonService } from '@services/common-service.service';
import { TranslateService } from '@ngx-translate/core';
import { DefaultLanguageService } from '@services/default-language.service';
import { FormGroup, FormControl, Validators } from '@angular/forms';
@Component({
  selector: 'app-input-password',
  templateUrl: './input-password.page.html',
  styleUrls: ['./input-password.page.scss'],
})
export class InputPasswordPage implements OnInit {

  
  public appUserId:string;

  public loading:boolean = false;

  public i18n:any = {};

  public validateForm:FormGroup;
  constructor(private router: Router,
    private route: ActivatedRoute,
    public http:HttpClient,
    private toastController: ToastController,
    private authTokenService:AuthTokenService,
    private defaultLanguageService:DefaultLanguageService,
    private encryptService:EncryptService,
    private translate: TranslateService,
    private commonService:CommonService) {
      this.i18n = this.commonService.initTranslate(['auth.inputPassword']);  
      this.validateForm = new FormGroup({
        'password':new FormControl('',[
          Validators.required,
          Validators.pattern('(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}')
        ]),
      });
  }

  get password(){return this.validateForm.get('password');}

  ngOnInit() {
    this.route.queryParams.subscribe((data)=>{
      this.appUserId = data.appUserId;
		})
  }

  signIn(){
    this.router.navigate(['/auth/signin-password']);
  }
  
  completeSignUp(){
    if(this.validateForm.invalid){
      for (const i in this.validateForm.controls) {
        this.validateForm.controls[i].markAsDirty();
        this.validateForm.controls[i].updateValueAndValidity();
      }
      return false;
    }
    this.loading = true;
    const httpOptions = {
      headers: new HttpHeaders({ 'Content-Type': 'application/json' })
    };
    var api = "/scm/app/saveAppUser.do";
    var requestJson = {
      'appUserId':this.appUserId,
      'password':this.validateForm.value.password,
      'appCode':environment.APP_CODE,
      'deviceId':this.authTokenService.getDeviceId()
    }
    this.http.post(api,requestJson,httpOptions).subscribe((response:CommonResponse) => {
      if(response.isSuccess){
        this.commonService.commonToast(ToastColor.success,this.i18n.signup_success);
        this.authTokenService.setAppUserId(this.appUserId);
        this.router.navigate(['/auth/fingerprint-setup']);
      }
      this.loading = false;
    },(errorResponse)=>{
      this.commonService.errorRespCommonToast(errorResponse,this.i18n);
      this.loading = false;
    });
  }

}
